#!/bin/bash

# NAS安装检查脚本
# 用于验证树莓派NAS配置是否正确

echo "=== 树莓派NAS安装检查 ==="
echo "正在检查NAS配置..."
echo ""

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# 检查函数
check_service() {
    local service_name=$1
    local display_name=$2
    
    if systemctl is-active --quiet $service_name; then
        echo -e "${GREEN}✅ $display_name: 运行中${NC}"
        return 0
    else
        echo -e "${RED}❌ $display_name: 未运行${NC}"
        return 1
    fi
}

check_file() {
    local file_path=$1
    local description=$2
    
    if [ -f "$file_path" ]; then
        echo -e "${GREEN}✅ $description: 存在${NC}"
        return 0
    else
        echo -e "${RED}❌ $description: 不存在${NC}"
        return 1
    fi
}

check_directory() {
    local dir_path=$1
    local description=$2
    
    if [ -d "$dir_path" ]; then
        echo -e "${GREEN}✅ $description: 存在${NC}"
        return 0
    else
        echo -e "${RED}❌ $description: 不存在${NC}"
        return 1
    fi
}

check_port() {
    local port=$1
    local description=$2
    
    if netstat -tuln | grep -q ":$port "; then
        echo -e "${GREEN}✅ $description (端口 $port): 开放${NC}"
        return 0
    else
        echo -e "${RED}❌ $description (端口 $port): 未开放${NC}"
        return 1
    fi
}

# 初始化计数器
total_checks=0
passed_checks=0

# 1. 检查系统信息
echo -e "${BLUE}📊 系统信息${NC}"
echo "主机名: $(hostname)"
echo "IP地址: $(hostname -I | awk '{print $1}')"
echo "系统时间: $(date)"
echo "运行时间: $(uptime -p)"
echo ""

# 2. 检查服务状态
echo -e "${BLUE}🔄 服务状态检查${NC}"
total_checks=$((total_checks + 2))
if check_service "smbd" "Samba服务"; then
    passed_checks=$((passed_checks + 1))
fi
if check_service "nfs-kernel-server" "NFS服务"; then
    passed_checks=$((passed_checks + 1))
fi
echo ""

# 3. 检查端口开放
echo -e "${BLUE}🔗 端口检查${NC}"
total_checks=$((total_checks + 3))
if check_port "445" "Samba端口"; then
    passed_checks=$((passed_checks + 1))
fi
if check_port "2049" "NFS端口"; then
    passed_checks=$((passed_checks + 1))
fi
if check_port "80" "Web服务端口"; then
    passed_checks=$((passed_checks + 1))
fi
echo ""

# 4. 检查配置文件
echo -e "${BLUE}📁 配置文件检查${NC}"
total_checks=$((total_checks + 4))
if check_file "/etc/samba/smb.conf" "Samba配置文件"; then
    passed_checks=$((passed_checks + 1))
fi
if check_file "/etc/exports" "NFS配置文件"; then
    passed_checks=$((passed_checks + 1))
fi
if check_file "/etc/udev/rules.d/99-usb-nas.rules" "USB自动挂载规则"; then
    passed_checks=$((passed_checks + 1))
fi
if check_file "/usr/local/bin/nas_status.sh" "状态检查脚本"; then
    passed_checks=$((passed_checks + 1))
fi
echo ""

# 5. 检查目录结构
echo -e "${BLUE}📂 目录结构检查${NC}"
total_checks=$((total_checks + 2))
if check_directory "/mnt/nas" "NAS挂载点"; then
    passed_checks=$((passed_checks + 1))
fi
if check_directory "/var/www/html/nas" "Web管理界面"; then
    passed_checks=$((passed_checks + 1))
fi
echo ""

# 6. 检查存储状态
echo -e "${BLUE}💾 存储状态检查${NC}"
if mountpoint -q /mnt/nas; then
    echo -e "${GREEN}✅ NAS存储已挂载${NC}"
    echo "存储信息:"
    df -h /mnt/nas
    passed_checks=$((passed_checks + 1))
else
    echo -e "${YELLOW}⚠️  NAS存储未挂载${NC}"
    echo "请连接USB硬盘或手动挂载"
fi
total_checks=$((total_checks + 1))
echo ""

# 7. 检查USB设备
echo -e "${BLUE}🔌 USB设备检查${NC}"
usb_devices=$(lsblk -o NAME,SIZE,FSTYPE,MOUNTPOINT | grep -E "sd[a-z]" | wc -l)
if [ $usb_devices -gt 0 ]; then
    echo -e "${GREEN}✅ 检测到 $usb_devices 个USB设备${NC}"
    lsblk -o NAME,SIZE,FSTYPE,MOUNTPOINT | grep -E "sd[a-z]"
    passed_checks=$((passed_checks + 1))
else
    echo -e "${YELLOW}⚠️  未检测到USB设备${NC}"
fi
total_checks=$((total_checks + 1))
echo ""

# 8. 检查网络连接
echo -e "${BLUE}🌐 网络连接检查${NC}"
if ping -c 1 8.8.8.8 >/dev/null 2>&1; then
    echo -e "${GREEN}✅ 网络连接正常${NC}"
    passed_checks=$((passed_checks + 1))
else
    echo -e "${RED}❌ 网络连接异常${NC}"
fi
total_checks=$((total_checks + 1))
echo ""

# 9. 检查日志文件
echo -e "${BLUE}📋 日志文件检查${NC}"
total_checks=$((total_checks + 2))
if check_file "/var/log/nas_monitor.log" "监控日志"; then
    passed_checks=$((passed_checks + 1))
fi
if check_file "/var/log/nas_backup.log" "备份日志"; then
    passed_checks=$((passed_checks + 1))
fi
echo ""

# 10. 检查定时任务
echo -e "${BLUE}⏰ 定时任务检查${NC}"
if crontab -l 2>/dev/null | grep -q "nas_monitor.sh"; then
    echo -e "${GREEN}✅ 监控定时任务已配置${NC}"
    passed_checks=$((passed_checks + 1))
else
    echo -e "${RED}❌ 监控定时任务未配置${NC}"
fi
total_checks=$((total_checks + 1))
echo ""

# 显示检查结果
echo -e "${BLUE}📊 检查结果汇总${NC}"
echo "总检查项目: $total_checks"
echo "通过检查: $passed_checks"
echo "失败检查: $((total_checks - passed_checks))"
echo "通过率: $((passed_checks * 100 / total_checks))%"
echo ""

# 根据检查结果给出建议
if [ $passed_checks -eq $total_checks ]; then
    echo -e "${GREEN}🎉 恭喜！NAS配置完全正常${NC}"
    echo ""
    echo "访问信息："
    echo "- Web管理界面: http://$(hostname -I | awk '{print $1}')/nas/"
    echo "- Samba共享: \\\\$(hostname -I | awk '{print $1}')"
    echo "- NFS共享: $(hostname -I | awk '{print $1}'):/mnt/nas/public"
    echo ""
    echo "管理命令："
    echo "- 查看状态: nas_status.sh"
    echo "- 重启服务: sudo systemctl restart smbd nfs-kernel-server"
elif [ $passed_checks -gt $((total_checks * 8 / 10)) ]; then
    echo -e "${YELLOW}⚠️  NAS配置基本正常，但有一些小问题${NC}"
    echo "建议检查失败的项目并修复"
else
    echo -e "${RED}❌ NAS配置存在问题，需要修复${NC}"
    echo "请检查失败的项目并重新运行配置脚本"
fi

echo ""
echo "=== 检查完成 ===" 